<?php
/**
 * Model for example application 1.
 */
class Model_App1 extends Psa_Plugin_Model {

	
	/**
	 * Maximum number in result used in methods below
	 */
	var $max_number = 150;


	/**
	 * Puts even numbers till $to_number in $psa_result object.
	 * @param int $to_number
	 */
	function even_numbers($to_number){

		// get max number
		$max_number = $this->get_max_number($to_number);
		
		// find even numbers
		for($i=0; $i <= $max_number; $i++){
			if($i % 2 == 0)
				$return[] = $i;
		}
		
		// put even numbers in psa_result object
		$this->psa_result->even_numbers = $return;
	}


	/**
	 * Puts odd numbers till $to_number in $psa_result object.
	 * @param int $to_number
	 */
	function odd_numbers($to_number = 120){

		// get max number
		$max_number = $this->get_max_number($to_number);
		
		// find odd numbers
		for($i=0; $i <= $max_number; $i++){
			if($i % 2 == 1)
				$return[] = $i;
		}
		
		// put odd numbers in psa_result object
		$this->psa_result->odd_numbers = $return;
	}


	/**
	 * Puts prim numbers till $to_number in $psa_result object.
	 * @param int $to_number
	 */
	function prim_numbers($to_number){
		
		// first prim number
		$prim[] = 1;
		
		// get max number
		$max_number = $this->get_max_number($to_number);
		
		// find prim numbers
		for($i=2; $i <= $max_number; $i++){
			$is_prim = 1;
			for($j=2; $j<$i; $j++){
				if($i%$j == 0){
					$is_prim = 0;
				}
			}
			if($is_prim)
				$prim[] = $i;
		}
		
		// put prim numbers in psa_result object
		$this->psa_result->prim_numbers = $prim;
	}

	
	/**
	 * Returns $this->max_number if $to_number is larger than $this->max_number.
	 * This is helper method for methods above.
	 * @param int $to_number
	 * @return int
	 */
	function get_max_number($to_number){

		if($to_number > $this->max_number)
			return $this->max_number;
		else
			return $to_number;
	}
}
?>